package com.mall.seckill_mall.controller;

import com.mall.seckill_mall.common.Result;
import com.mall.seckill_mall.entity.Order;
import com.mall.seckill_mall.enums.CodeMessage;
import com.mall.seckill_mall.service.OrderService;
import com.mall.seckill_mall.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/goods/{goodsId}")
    public Result getOrder(@PathVariable("goodsId") String goodsId
            , @CookieValue("user_token") String userToken) {
        Order order = orderService.getOrder(userToken, goodsId);
        if(order == null) {
            return ResultUtil.error(CodeMessage.ORDER_NOT_EXISTS);
        }
        return ResultUtil.success(order);
    }

    @GetMapping("/{orderId}")
    public Result getOrderByOid(@PathVariable("orderId") String orderId) {
        Order order = orderService.getOrderByOrderId(orderId);
        if(order == null) {
            return ResultUtil.error(CodeMessage.ORDER_NOT_EXISTS);
        }
        return ResultUtil.success(order);
    }
}
